package com.gzdh.dishrecognition.ui.adapter.inputNumber;

import android.content.Context;
import android.graphics.Color;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.gzdh.dishrecognition.R;
import com.gzdh.dishrecognition.model.ProductCartModel;

import java.util.ArrayList;
import java.util.List;

public class InputNumberAdapter extends RecyclerView.Adapter<InputNumberAdapter.MyViewHolder> {

    private LayoutInflater inflater;

    private Context mContext;

    private OnReshow onReshow;

    public InputNumberAdapter(Context context) {
        inflater = LayoutInflater.from(context);
        mContext = context;
    }

    public void setOnReshow(OnReshow onReshow) {
        this.onReshow = onReshow;
    }

    private List<ProductCartModel> productCartModelList = new ArrayList<>();

    public List<ProductCartModel> getProductCartModelList() {
        return productCartModelList;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_str2, parent, false);
        return new MyViewHolder(itemView);
    }

    public interface OnReshow {
        void reShow();
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        ProductCartModel productCartModel = productCartModelList.get(position);
        holder.nameTv.setText("￥" + productCartModel.getPrice());
        holder.nameTv.setBackgroundColor(Color.WHITE);
        holder.nameTv.setGravity(Gravity.CENTER);
        holder.nameTv.setPadding(10, 10, 10, 10);
        holder.nameTv.setOnClickListener(view -> {
            ProductCartModel productCartModel1 = productCartModelList.get(position);
            productCartModelList.remove(productCartModel1);
            if (onReshow != null) {
                onReshow.reShow();
            }
        });
    }

    //数据源的数量
    @Override
    public int getItemCount() {
        return productCartModelList.size();
    }

    public void setProductCartModelList(List<ProductCartModel> productCartModelList) {
        this.productCartModelList = productCartModelList;
        notifyDataSetChanged();
    }

    static class MyViewHolder extends RecyclerView.ViewHolder {
        private TextView nameTv;


        public MyViewHolder(View itemView) {
            super(itemView);
            nameTv = (TextView) itemView.findViewById(R.id.nameTv);

        }
    }
}
